package jkoa.middleware;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import cn.hutool.core.io.FileUtil;
import cn.hutool.setting.Setting;
import jkoa.core.Context;
import jkoa.core.Middleware;
import jkoa.core.Next;

public class MySqlMiddleware implements Middleware {
    private JdbcTemplate jdbcTemplate;

    public MySqlMiddleware() {
        String mysqlConfigPath = "mysql.properties";
        if (FileUtil.exist(mysqlConfigPath)) {
            Setting setting = new Setting(mysqlConfigPath);
            String url = setting.getStr("mysql.url");
            String username = setting.getStr("mysql.username");
            String password = setting.getStr("mysql.password");
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            jdbcTemplate = new JdbcTemplate(dataSource);
        }
    }

    @Override
    public void handle(Context ctx, Next next) {
        // 这里可以根据需求处理与数据库相关的请求
        next.execute(ctx);
    }
}